const db = require('../db/index');

// 导入fs处理文件
const fs = require('fs');

// 上传轮播图
exports.uploadSwiper = (req, res) => {
  let oldName = req.files[0].filename;
  let newName = Buffer.from(req.files[0].originalname, 'latin1').toString('utf8');
  // fs.renameSync( oldPath, newPath )
  fs.renameSync(`./public/upload/${oldName}`, `./public/upload/${newName}`);
  // setting 这是表名字
  const sql = 'update setting set set_value = ? where set_name = ?';
  db.query(sql, [`http://127.0.0.1:3000/upload/${newName}`, req.body.set_name], (err, results) => {
    if (err) return res.cc(err)
    if (results.affectedRows !== 1) return res.cc('头像上传失败')
    res.send({
      status: 0,
      message: '上传轮播图成功',
      url: `http://127.0.0.1:3000/upload/${newName}`
    })
  })
}

// 获取所有轮播图
exports.getAllSwiper = (req, res) => {
  let sql = "select * from setting where set_name like 'swiper%'"
  db.query(sql, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      results
    })
  })
}

// 获取公司名称
exports.getCompanyName = (req, res) => {
  let sql = "select * from setting where set_name = '公司名称'";
  db.query(sql, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      results: results[0].set_value
    })
  })
}
// 修改公司名称
exports.updateCompanyName = (req, res) => {
  let sql = "update setting set set_value = ? where set_name = '公司名称'";
  db.query(sql, req.body.set_value, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      message: '修改公司名称成功'
    })
  })
}
// 修改公司介绍/战略
exports.updateCompanyOthers = (req, res) => {
  let sql = "update setting set set_text = ? where set_name = ?";
  db.query(sql, [req.body.set_text, req.body.set_name], (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      message: '修改成功'
    })
  })
}

// 获取公司介绍/战略
exports.getCompanyOthers = (req, res) => {
  let sql = "select * from setting where set_name = ?";
  db.query(sql, req.body.set_name, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      results: results[0].set_text
    })
  })
}

// 获取公司所有信息
exports.getAllCompanyInfo = (req, res) => {
  let sql = "select * from setting where set_name like '公司%'";
  db.query(sql, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      results
    })
  })
}

// 设置部门信息
exports.setDepartment = (req, res) => {
  let sql = 'update setting set set_value = ? where set_name = "部门设置"';
  db.query(sql, req.body.set_value, (err, results) => {
    if (err) return res.cc(err)
    if (results.affectedRows !== 1) return res.cc('部门设置失败')
    res.send({
      status: 0,
      message: '部门设置成功'
    })
  })
}
// 获取部门信息
exports.getDepartmentList = (req, res) => {
  let sql = 'select * from setting where set_name = "部门设置"';
  db.query(sql, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      results
    })
  })
}

// 设置产品信息
exports.setProduct = (req, res) => {
  let {
    set_value
  } = req.body;
  let sql = 'update setting set set_value = ? where set_name="产品设置"';
  db.query(sql, set_value, (err, results) => {
    if (err) return res.cc(err)
    if (results.affectedRows !== 1) return res.cc('产品设置失败')
    res.send({
      status: 0,
      message: '产品设置成功'
    })
  })
}

//获取产品信息
exports.getProductList = (req, res) => {
  let sql = 'select * from setting where set_name = "产品设置"';
  db.query(sql, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      results
    })
  })
}